<?php

namespace app\modules\controllers;
use yii;
use yii\web\Controller;
use app\modules\models\Admin;
use yii\data\Pagination;


/**
 * Default controller for the `admin` module
 */
class ManageController extends Controller
{
	public $layout='layout1';
    /**
     * Renders the index view for the module
     * @return string
     */
   public $enableCsrfValidation = false;
   
    public function actionChangeemail()
    {
 
      $model=Admin::find()->where('adminuser=:user',[':user'=>'admin'])->one();
        return $this->render('Changeemail',['model'=>$model]);
    }
    public function actionChangepass()
    {
      
      $model=Admin::find()->where('adminuser=:user',[':user'=>YII::$app->session['admin']['adminuser']])->one();
      if(Yii::$app->request->isPost){

        $post=Yii::$app->request->post();
        if($model->changPass($post)){
          Yii::$app->session->setFlash('info','修改成功');
        }
      }
      $model->adminpass='';
      $model->repass='';
        return $this->render('Changepass',['model'=>$model]);
    }

    public function actionMailchangepass()
    {
      $this->layout=false;

      $time=Yii::$app->request->get('timestamp');
      $adminuser= Yii::$app->request->get("adminuser");
      $token=Yii::$app->request->get("token");
      $model=new Admin;
      $mytoken=$model->creatToken($adminuser,$time);
      if($token!=$mytoken){
        $this->redirect(['public/login']);
        Yii::$app->end();
      }
      if(time()-$time>300){
         $this->redirect(['public/login']);
        Yii::$app->end();
      }
      if(Yii::$app->request->isPost){

        $post=Yii::$app->request->post();

        if($model->changPass($post)){

          Yii::$app->session->setFlash('info','密码修改成功');

          

        }
      }
      $model->adminuser=$adminuser;
        return $this->render('Mailchangepass',['model'=>$model]);
    }

    public function actionManagers()
    {

         
         $model=Admin::find();
         $count=$model->count();
         $pageSize=Yii::$app->params['pageSize']['manage'];
         $pager=new Pagination(['totalCount'=> $count,'pageSize'=>$pageSize]);
         $managers=$model->offset($pager->offset)->limit($pager->limit)->all();

        return $this->render('Managers',['managers'=>$managers,'pager'=>$pager]);
    }

     public function actionReg()
    {
      $model=new Admin;

      if(Yii::$app->request->isPost){
        $post=Yii::$app->request->post();
 
        if($model->reg($post)){
          Yii::$app->session->setFlash('info','添加成功');
          $model->adminpass='';
         $model->repass='';
        }else{
          Yii::$app->session->setFlash('info','添加失败');
        }


      }
        return $this->render('Reg',['model'=>$model]);
      }
      public function actionDel(){
         $adminid=(int)Yii::$app->request->get('adminid');
         if(empty($adminid)){
          $this->redirect(['manage/managers']);
         }
         $model=new Admin;
         if($model->deleteAll('adminid=:id',[':id'=>$adminid])){
           Yii::$app->session->setFlash('info','删除成功');
           $this->redirect(['manage/managers']);
         }
      }

}
